home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1994 April / Inside Multimedia CD-ROM (April 1994).iso / prg / gs / gssource.exe / ZPROPS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-24  |  7.0 KB  |  256 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zprops.c */
  21. /* Device property operators */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "alloc.h"
  25. #include "dict.h"
  26. #include "errors.h"
  27. #include "oper.h"
  28. #include "name.h"
  29. #include "store.h"
  30. #include "gsprops.h"
  31. #include "gsmatrix.h"            /* for gxdevice.h */
  32. #include "gxdevice.h"
  33.  
  34. /* Forward references */
  35. private int props_to_stack(P3(const gs_prop_item *, os_ptr, int));
  36. private int props_from_stack(P3(gs_prop_item *, const_os_ptr, int));
  37.  
  38. /* getdeviceprops */
  39. int
  40. zgetdeviceprops(os_ptr op)
  41. {    gx_device *dev;
  42.     gs_prop_item *plist;
  43.     int count;
  44.     int code;
  45.     check_type(*op, t_device);
  46.     dev = op->value.pdevice;
  47.     count = (*dev->procs->get_props)(dev, NULL);
  48.     plist = (gs_prop_item *)alloc(count, sizeof(gs_prop_item), "getdeviceprops");
  49.     if ( plist == 0 ) return_error(e_VMerror);
  50.     (*dev->procs->get_props)(dev, plist);
  51.     code = props_to_stack(plist, op + 1, count);
  52.     alloc_free((char *)plist, count, sizeof(gs_prop_item), "getdeviceprops");
  53.     if ( code >= 0 )
  54.        {    make_mark(op);
  55.         push(code);
  56.         code = 0;
  57.        }
  58.     return code;
  59. }
  60.  
  61. /* putdeviceprops */
  62. int
  63. zputdeviceprops(os_ptr op)
  64. {    gx_device *dev;
  65.     gs_prop_item *plist;
  66.     os_ptr mp;
  67.     int count, acount = 0;
  68.     int code;
  69.     check_type(*op, t_device);
  70.     dev = op->value.pdevice;
  71.     for ( mp = op - 1; !r_has_type(mp, t_mark); mp-- )
  72.        {    if ( mp <= osbot ) return e_unmatchedmark;
  73.         switch ( r_type(mp) )
  74.            {
  75.         case t_array:
  76.         case t_mixedarray:
  77.         case t_shortarray:
  78.             acount += r_size(mp);
  79.            }
  80.        }
  81.     count = op - mp - 1;
  82.     if ( count & 1 ) return e_rangecheck;
  83.     count >>= 1;
  84.     plist = (gs_prop_item *)alloc(count + acount, sizeof(gs_prop_item), "putdeviceprops");
  85.     if ( plist == 0 ) return_error(e_VMerror);
  86.     code = props_from_stack(plist, mp + 1, count);
  87.     if ( code >= 0 )
  88.         code = (*dev->procs->put_props)(dev, plist, count + acount);
  89.     alloc_free((char *)plist, count + acount, sizeof(gs_prop_item), "putdeviceprops");
  90.     if ( code >= 0 )
  91.        {    *mp = *op;
  92.         osp = op = mp;
  93.        }
  94.     return code;
  95. }
  96.  
  97. /* ------ Initialization procedure ------ */
  98.  
  99. op_def zprops_op_defs[] = {
  100.     {"1getdeviceprops", zgetdeviceprops},
  101.     {"2putdeviceprops", zputdeviceprops},
  102.     op_def_end(0)
  103. };
  104.  
  105. /* ------ Internal routines ------ */
  106.  
  107. /* Get properties from a property list to the stack. */
  108. private int
  109. props_to_stack(const gs_prop_item *plist, os_ptr op0, int count)
  110. {    const gs_prop_item *pi;
  111.     os_ptr op;
  112.     int i;
  113.     int code;
  114.     for ( op = op0, pi = plist, i = count; i != 0; pi++, i-- )
  115.        {    ref value;
  116.         const char *nstr = pi->pname;
  117.         int nlen = pi->name_size;
  118.         if ( nstr == 0 ) continue;    /* no name, skip */
  119.         if ( ostop - op < 2 ) return e_stackoverflow;
  120.         if ( nlen < 0 ) nlen = strlen(nstr);
  121.         code = name_ref((const byte *)nstr, nlen, op, 0);
  122.         if ( code < 0 ) return code;
  123.         switch ( pi->type )
  124.            {
  125.         case (int)prt_int:
  126.             make_int(&value, pi->value.i);
  127.             break;
  128.         case (int)prt_float:
  129.             make_real(&value, pi->value.f);
  130.             break;
  131.         case (int)prt_bool:
  132.             make_bool(&value, pi->value.b);
  133.             break;
  134.         case (int)prt_string:
  135.            {    ushort size = pi->value.a.size;
  136.             char *str;
  137.             if ( size == (ushort)(-1) )
  138.                 size = strlen(pi->value.a.p.s);
  139.             str = alloc(size, 1, "props_to_stack(string)");
  140.             if ( str == 0 ) return e_VMerror;
  141.             memcpy(str, pi->value.a.p.s, size);
  142.             make_tasv(&value, t_string, a_all, size, bytes, (byte *)str);
  143.            }    break;
  144.         case (int)prt_int_array:
  145.         case (int)prt_float_array:
  146.            {    uint size = pi->value.a.size;
  147.             ref *arefs = alloc_refs(size, "props_to_stack(array)");
  148.             uint j;
  149.             gs_prop_item *pv = pi->value.a.p.v;
  150.             if ( arefs == 0 ) return e_VMerror;
  151.             make_tasv_new(&value, t_array, a_all, size, refs, arefs);
  152.             for ( j = 0; j < size; j++, arefs++, pv++ )
  153.               if ( pi->type == prt_int_array )
  154.                 make_int_new(arefs, pv->value.i);
  155.               else
  156.                 make_real_new(arefs, pv->value.f);
  157.            }    break;
  158.         default:
  159.             return e_typecheck;
  160.            }
  161.         ref_assign(op + 1, &value);
  162.         op += 2;
  163.        }
  164.     return op - op0;
  165. }
  166.  
  167. /* Set properties from the stack. */
  168. /* Returns the number of elements copied. */
  169. /* Entries with non-name keys are not copied; */
  170. /* entries with invalid values are copied with status = pv_typecheck. */
  171. private int
  172. props_from_stack(gs_prop_item *plist /* [count + acount] */, const_os_ptr op0,
  173.   int count)
  174. {    gs_prop_item *pi = plist;
  175.     gs_prop_item *pai = plist + count;
  176.     const_os_ptr op = op0 + 1;
  177.     for ( ; count; op += 2, count-- )
  178.        {    ref sref;
  179.         if ( !r_has_type(op - 1, t_name) ) return e_typecheck;
  180.         name_string_ref(op - 1, &sref);
  181.         pi->pname = (char *)sref.value.bytes;
  182.         pi->name_size = r_size(&sref);
  183.         pi->status = pv_set;
  184.         switch ( r_type(op) )
  185.            {
  186.         case t_null:
  187.             pi->type = prt_null;
  188.             break;
  189.         case t_integer:
  190.             pi->type = prt_int;
  191.             pi->value.i = op->value.intval;
  192.             break;
  193.         case t_real:
  194.             pi->type = prt_float;
  195.             pi->value.f = op->value.realval;
  196.             break;
  197.         case t_boolean:
  198.             pi->type = prt_bool;
  199.             pi->value.b = op->value.index;
  200.             break;
  201.         case t_name:
  202.             name_string_ref(op, &sref);
  203.             goto nst;
  204.         case t_string:
  205.             ref_assign(&sref, op);
  206.             pi->type = prt_string;
  207.             pi->value.a.p.s = (char *)op->value.bytes;
  208. nst:            pi->value.a.size = r_size(&sref);
  209.             break;
  210.         case t_array:
  211.         case t_mixedarray:
  212.         case t_shortarray:
  213.            {    uint size = r_size(op);
  214.             uint i;
  215.             gs_prop_item *pv;
  216.             gs_prop_type tv = prt_int;
  217.             pi->type = prt_int_array;
  218.             pi->value.a.p.v = pai;
  219.             pi->value.a.size = size;
  220. top:            pv = pai;
  221.             for ( i = 0; i < size; i++ )
  222.                {    ref rnum;
  223.                 array_get(op, (long)i, &rnum);
  224.                 pv->pname = 0;
  225.                 pv->type = tv;
  226.                 switch ( r_type(&rnum) )
  227.                    {
  228.                 case t_real:
  229.                     if ( tv == prt_int )
  230.                        {    tv = prt_float;
  231.                         pi->type = prt_float_array;
  232.                         goto top;
  233.                        }
  234.                     pv++->value.f = rnum.value.realval;
  235.                     break;
  236.                 case t_integer:
  237.                     if ( tv == prt_int )
  238.                       pv++->value.i = rnum.value.intval;
  239.                     else
  240.                       pv++->value.f = rnum.value.intval;
  241.                     break;
  242.                 default:
  243.                     pi->status = pv_typecheck;
  244.                    }
  245.                }
  246.  
  247.             pai = pv;
  248.            }    break;
  249.         default:
  250.             pi->status = pv_typecheck;
  251.            }
  252.         pi++;
  253.        }
  254.     return 0;
  255. }
  256.